home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / newmat08.zip / SOLUTION.H < prev    next >
C/C++ Source or Header  |  1995-01-11  |  3KB  |  82 lines

  1. //$$ solution.h                      // solve routines
  2.  
  3. #include "boolean.h"
  4. #include "myexcept.h"
  5.  
  6. // Solve the equation f(x)=y for x where f is a monotone continuous
  7. // function of x
  8. // Essentially Brent's method
  9.  
  10. // You need to derive a class from R1_R1 and override "operator()"
  11. // with the function you want to solve.
  12. // Use an object from this class in OneDimSolve
  13.  
  14. class R1_R1
  15. {
  16.    // the prototype for a Real function of a Real variable
  17.    // you need to derive your function from this one and put in your
  18.    // function for operator() at least. You probably also want to set up a
  19.    // constructor to put in additional parameter values (that won't
  20.    // vary during the solve)
  21.  
  22. protected:
  23.    Real x;                             // Current x value
  24.  
  25. public:
  26.    Boolean IsValid() { return TRUE; }  // is the current x value OK
  27.                                        // not used at present
  28.    virtual Real operator()() = 0;      // function value at current x
  29.    virtual void Set(Real X) { x = X; } // set current x
  30.    Boolean IsValid(Real X) { Set(X); return IsValid(); }
  31.                                        // set x, check OK
  32.                                        // not used at present
  33.    Real operator()(Real X) { Set(X); return operator()(); }
  34.                                        // set x, return value
  35. };
  36.  
  37. class SolutionException : public Exception
  38. {
  39.    static int action;
  40. protected:
  41.    SolutionException() {}
  42. public:
  43.    static long st_type() { return 5; }
  44.    long type() const { return 5; }
  45.    SolutionException(char*);
  46.    SolutionException(const SolutionException&) {}
  47.    static void SetAction(int a) { action=a; }
  48. };
  49.  
  50. class OneDimSolve
  51. {
  52.    R1_R1& function;                     // reference to the function
  53.    Real acc;                            // accuracy
  54.    int lim;                             // maximum number of iterations
  55.  
  56. public:
  57.    OneDimSolve(R1_R1& f, Real Acc = 0.0001) : function(f), acc(Acc) {}
  58.                        // f is an R1_R1 function
  59.    Real Solve(Real Y, Real X, Real Dev, int Lim=100);
  60.                        // Solve for x in Y=f(x)
  61.                        // X is the initial trial value of x
  62.                        // X+Dev is the second trial value
  63.                        // program returns a value of x such that
  64.                        // |Y-f(x)| < acc
  65.  
  66. private:
  67.    Real x[3], y[3];                         // Trial values of X and Y
  68.    int L,C,U,Last;                          // Locations of trial values
  69.    int vpol, hpol;                          // polarities
  70.    Real YY;                                 // target value
  71.    int i;
  72.    void LookAt(int);                        // get new value of function
  73.    Boolean Finish;                          // TRUE if LookAt finds conv.
  74.    void VFlip();
  75.    void HFlip();
  76.    void Flip();
  77.    void State(int I, int J, int K) { L=I; C=J; U=K; }
  78.    void Linear(int, int, int);
  79.    void Quadratic(int, int, int);
  80. };
  81.  
  82.